<template>
	<a-card :bordered="false" class="mb-3">
		<exclamation-circle-filled style="font-size: 16px; color: gray" />
		<a-typography-text type="primary" class="ml-2"
			>若选择自动释放，员工在指定时间内未将企业下的任一联系方式转为线索/客户联系人，该企业的所有联系方式将被释放，被释放的企业资源可通过智能拓客重新领取</a-typography-text
		>
	</a-card>
	<a-card :bordered="false" class="mb-3 pl-4">
		<a-form ref="formRef" :model="formData" :rules="formRules">
			<h3>企业资源释放</h3>
			<a-form-item name="releaseType">
				<a-radio-group v-model:value="formData.releaseType" :options="releaseOptions" />
			</a-form-item>
			<a-form-item v-if="formData.releaseType === resourceReleaseType.AUTO" name="exceedDays">
				超过
				<a-input-number v-model:value="formData.exceedDays" min="1" class="mx-2" />
				天未转化成客户，该企业下所有的联系方式将被释放
			</a-form-item>
			<div>
				<a-button class="mt-5" type="primary" :loading="formLoading" @click="onSubmit">保存</a-button>
			</div>
		</a-form>
	</a-card>
</template>

<script setup name="Recycle">
	import tool from '@/utils/tool'
	import resourceReleaseType from '@/views/biz/crm/recycle/enum/resourceReleaseType'
	import recycleApi from '@/api/biz/crm/recycleApi'
	import { required } from '@/utils/formRules'

	const formLoading = ref(false)

	const formRef = ref()
	// 表单数据，也就是默认给一些数据
	const formData = ref({
		releaseType: resourceReleaseType.NO,
		exceedDays: null
	})
	// 默认要校验的
	const formRules = {
		exceedDays: [required('请输入超过天数')]
	}

	const releaseOptions = tool.dictList('RESOURCE_ RELEASE_TYPE')

	onMounted(() => {
		recycleApi.detail().then((data) => {
			formData.value = data
		})
	})

	const onSubmit = () => {
		formRef.value.validate().then(() => {
			formLoading.value = true
			const { releaseType, exceedDays } = formData.value
			recycleApi
				.submitForm({
					releaseType,
					exceedDays: releaseType === resourceReleaseType.NO ? null : exceedDays
				})
				.then(() => {})
				.finally(() => {
					formLoading.value = false
				})
		})
	}
</script>

<style scoped></style>
